package com.xjx.util.util;

import com.xjx.util.domain.User;
import org.apache.commons.io.FileUtils;

import javax.servlet.annotation.WebServlet;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserUtils {
    static File file = new File("/Users/edy/myself/new1.txt");

    /**
     * 读取文件中所有的用户
     */
    public static List<User> getAll() throws IOException, IOException {
        List<String> strList = FileUtils.readLines(file, "UTF-8");

        List<User> userList = new ArrayList<>();
        for (String s : strList)
        {
            User user = strToUser(s);
            userList.add(user);
        }
        return userList;
    }

    /**
     * 添加一个用户
     */
    public static void add(User user) throws IOException {
        //String userStr =user.getId()+" "+user.getName()+" "+user.getAge()+" "+user.getSex()+" "+user.getAddress()+"\n";
        FileUtils.writeStringToFile(file,userToStr(user),"UTF-8",true);
    }

    /**
     * 把一个User对象转换成文档中的一行数据
     * @param user
     * @return
     */
    public static String userToStr(User user)
    {
        return user.getId()+" "+user.getName()+" "+user.getAge()+" "+user.getSex()+" "+user.getAddress()+"\n";
    }

    /**
     * String转换成 User对象
     * 1 张三 19 男 南京-----user
     */
    public static User strToUser(String s)
    {
        String[] strArr = s.split(" ");
        User user = new User();
        user.setId(strArr[0]);
        user.setName(strArr[1]);
        user.setAge(strArr[2]);
        user.setSex(strArr[3]);
        user.setAddress(strArr[4]);

        return user;
    }

    /**
     * 通过id从文件中删除一个用户
     */
    public static void delete(String id) throws IOException {
        // 调用getAll，获取所有用户信息
        List<User> userList = getAll();
        //可以完全清除文档
        FileUtils.writeStringToFile(file,"","UTF-8");

        for(User user : userList)
        {
            //如果user的id和传入的id一样，这一行就不用写入，否则就写入到文档
            if(!user.getId().equals(id))
            {
                FileUtils.writeStringToFile(file,userToStr(user),"UTF-8",true);
            }
        }
    }

    /**
     * 根据id号，查询一个用户
     */
    public static User findOne(String id) throws IOException {
        List<User> userList = getAll();
        for (User user : userList)
        {
            if(user.getId().equals(id))
            {
                return user;
            }
        }
        return null;
    }


    /**
     * 根据ID，修改这个用户的其他信息
     */
    public static void update(User user) throws IOException {
        List<User> userList = getAll();

        //完全清除文档
        FileUtils.writeStringToFile(file,"","UTF-8");

        for(User user1 : userList)
        {
            //如果user的id和传入的id一样，这一行就不用写入，否则就写入到文档
            if(user.getId().equals(user1.getId()))
            {
                FileUtils.writeStringToFile(file,userToStr(user),"UTF-8",true);
            }
            else
            {
                FileUtils.writeStringToFile(file,userToStr(user1),"UTF-8",true);
            }
        }
    }

    public static void main(String[] args) throws IOException {

        //System.out.println(getAll());
        User user = new User("3","zhangsan","19","男","南京");

        add(user);
    }
}
